package com.chwn.curiousdiary.ui.search;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.api.IHomeApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.bean.HotSearchBean;
import com.chwn.curiousdiary.bean.StoryListBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.mvp.rx.HttpResultException;
import com.chwn.harulib.mvp.rx.RxSchedulers;

import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Myshikin on 2017/8/8.
 */

public class SearchPresenter extends SearchContract.Presenter {

    /**
     * 故事搜索
     */
    private Observer<HttpResult<List<StoryListBean>>> searchObserver = new Observer<HttpResult<List<StoryListBean>>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<List<StoryListBean>> httpResult) {
            List<StoryListBean> data = httpResult.getResdata();
            if (httpResult.getResult() == 1 && data != null && data.size() > 0) {
                mView.showSearchStories(data);
            } else {
                mView.onRequestError(new HttpResultException(Constant.RESULT_EMPTY , "无更多数据"));
            }
        }
    };

    /**
     * 热搜关键词
     */
    private Observer<HttpResult<List<HotSearchBean>>> hotSearchObserver = new Observer<HttpResult<List<HotSearchBean>>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<List<HotSearchBean>> httpResult) {
            List<HotSearchBean> data = httpResult.getResdata();
            if (httpResult.getResult() == 1 && data != null && data.size() > 0) {
                mView.showHotSearch(data);

            }
        }
    };

    @Override
    public void getSearchStories(int page, String searchKey) {
        User user = DiaryApplication.mInstance.getmUser();
        String userToken = "";
        if (user != null) {
            userToken = user.getUserToken();
        }
        NetWork.getInstance()
                .getApiService(IHomeApi.class ,true)
                .storySearch(searchKey , userToken, page, Constant.PAGE_COUNT , Constant.OSTYPE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(searchObserver);
    }

    @Override
    public void getHotSearch() {

        NetWork.getInstance()
                .getApiService(IHomeApi.class ,true)
                .getHotSearch(Constant.OSTYPE)
                .compose(RxSchedulers.<HttpResult<List<HotSearchBean>>>io_main())
                .subscribe(hotSearchObserver);
    }
}
